$(function() {
  function selectNav() {
    // 进入页面，选中指定导航
    var group = parseInt($Common.getUrlParams().group) || 0;
    if (group) {
      var $subgroup = $('.submenu[data-group="' + group + '"]');
      var links = $subgroup.find('a'),
        url = window.location.href;
      //$subgroup.removeClass('hide');
      // 选中子菜单
      for (var i = 0; i < links.length; i++) {
        if (url.indexOf(links[i].href) >= 0) {
          links.eq(i).addClass('active');
        }
      }
    } else {
      $('.submenu').addClass('hide');
    }
    $('#nav .link2')
      .eq(group)
      .addClass('active');
  }
  // selectNav();
  // hover
  $('#nav .link2').hover(function() {
    var _this = $(this);

    _this
      .addClass('active')
      .siblings('a')
      .removeClass('active');

    if (_this.index() === 0 || _this.index() === 2 || _this.index() === 3) {
      $('#nav .submenu').addClass('hide');
      _this.siblings('a').removeClass('current');
    } else {
      // console.log('label:', _this.data('label'));
      // _this.siblings('a').removeClass('current');
      // $('#nav .submenu').addClass('hide');

      _this
        .addClass('current')
        .siblings('a')
        .removeClass('current');

      $('#nav .submenu[data-label="' + _this.data('label') + '"]')
        .removeClass('hide')
        .siblings('div')
        .addClass('hide');
    }
  });
  $('#nav').hover(
    function() {},
    function() {
      $(this)
        .find('.active')
        .removeClass('active current');
      $(this)
        .find('.submenu')
        .addClass('hide');

      // selectNav();
    }
  );

  $(document).on('click', '.back2top', function() {
    $('html, body').animate(
      {
        scrollTop: 0
      },
      800
    );
  });

  $(window).scroll(function() {
    var top = $(this).scrollTop();
    if (top >= $('#nav').height()) {
      $('.header').addClass('active');
    } else {
      $('.header').removeClass('active');
    }

    top > 200 ? $('.back2top').addClass('is-active') : $('.back2top').removeClass('is-active');
  });

  console.log('%cHi，欢迎访问环球黑卡！环球黑卡是一个很温馨的大家庭哦~ 欢迎您的加入哦^_^', 'color: #C1973D');
  console.log('%c环球黑卡招聘简历提交：http://t.cn/Ro5B3GI （你将有机会直接获得面试资格）', 'color:red');
});

window.$Common = {
  showToast: function(msg, duration) {
    duration = duration || 2000;
    var node = document.getElementById('toast');
    if (!node) {
      node = document.createElement('div');
    }
    node.id = 'toast';
    node.className = 'toast zF';
    node.innerHTML = msg;
    document.body.appendChild(node);

    setTimeout(function() {
      node.className = 'toast hide zF';
    }, duration);
  },

  /**
   * 格式化日期
   * @param time {String} 可以转换成日期的格式字符串
   * @return {String} 返回格式化的日期字符串
   */
  formatDate: function(time) {
    time = time || new Date().getTime();
    var timeStr = '',
      cmtDateObj = new Date(time),
      cmtYear = cmtDateObj.getFullYear(),
      cmtMonth = cmtDateObj.getMonth() + 1,
      cmtDate = cmtDateObj.getDate(),
      interval = (new Date().getTime() - time) / 60000; // 分钟
    cmtDateObj.setHours(24);
    cmtDateObj.setMinutes(0);
    cmtDateObj.setSeconds(0);
    cmtDateObj.setMilliseconds(0);
    var yTime = (cmtDateObj.getTime() - time) / 60000 + 24 * 60,
      tdbyTime = yTime + 24 * 60;
    if (interval < 1) {
      timeStr = '刚刚';
    } else if (interval < 60) {
      timeStr = Math.floor(interval) + '分钟前';
    } else if (interval < 60 * 24) {
      timeStr = Math.floor(interval / 60) + '小时前';
    } else if (interval <= yTime) {
      timeStr = '昨天';
    } else if (interval <= tdbyTime) {
      timeStr = '2天前';
    } else {
      timeStr = cmtYear + '.' + (cmtMonth < 10 ? '0' + cmtMonth : cmtMonth) + '.' + (cmtDate < 10 ? '0' + cmtDate : cmtDate);
    }
    return timeStr;
  },

  getUrlParams: function() {
    var search = window.location.search.substring(1),
      queryObject = {};
    for (var t = search.split('&'), i = 0; i < t.length; i++) {
      var kv = t[i].split('=');
      queryObject[kv[0]] = kv[1];
    }
    return queryObject;
  }
};
